package com.faithbj.process;

import com.faithbj.common.model.CheckTotal;
import com.faithbj.common.model.fm.FamilyMember;

import java.util.Date;

import static org.apache.commons.lang3.StringUtils.isBlank;

/**
 * User: xiongyuanwei
 * Date: 12-7-14
 * Time: 下午8:39
 */
public class CheckTotalCondition implements Condition<CheckTotal> {

    /** 受理编号 */
    private String acceptId;

    /** 申请人代表姓名 */
    private String applicantName;

    /** 证件类型 */
    private String idType;

    /** 证件号 */
    private String idNumber;

    /** 受理日期开始 */
    private Date acceptDateFrom;

    @Override
    public boolean isSatisfiedBy(CheckTotal checkTotal) {
        boolean result = false;
        String acceptId = checkTotal.getId();
        if (isBlank(this.acceptId) || acceptId != null && acceptId.equals(this.acceptId)) {
            Date acceptDate = checkTotal.getHandleTime();
            if (this.acceptDateFrom == null || acceptDate != null && !acceptDate.before(this.acceptDateFrom)) {
                if (!needsFamilyMember()) {
                    result = true;
                } else {
                    FamilyMember fm = checkTotal.getFamilyMember();
                    if (fm != null) {
                        String applicantName = checkTotal.getFamilyMember().getName();
                        if (isBlank(this.applicantName) || applicantName != null && applicantName.equals(this.applicantName)) {
                            String idType = fm.getIdType();
                            if (isBlank(this.idType) || idType != null && idType.equals(this.idType)) {
                                String idNumber = fm.getIdNumber();
                                if (isBlank(this.idNumber) || idNumber != null && idNumber.equals(this.idNumber)) {
                                    result = true;
                                }
                            }
                        }
                    }
                }
            }
        }
        return result;
    }

    public boolean isEmpty() {
        return isBlank(acceptId) && isBlank(applicantName) && isBlank(idType) &&
                isBlank(idNumber) && acceptDateFrom == null;
    }

    public boolean needsFamilyMember() {
        return applicantName != null || idType != null || idNumber != null;
    }

    public String getAcceptId() {
        return acceptId;
    }

    public void setAcceptId(String acceptId) {
        this.acceptId = acceptId;
    }

    public String getApplicantName() {
        return applicantName;
    }

    public void setApplicantName(String applicantName) {
        this.applicantName = applicantName;
    }

    public String getIdType() {
        return idType;
    }

    public void setIdType(String idType) {
        this.idType = idType;
    }

    public String getIdNumber() {
        return idNumber;
    }

    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }

    public Date getAcceptDateFrom() {
        return acceptDateFrom;
    }

    public void setAcceptDateFrom(Date acceptDateFrom) {
        this.acceptDateFrom = acceptDateFrom;
    }
}
